<?php


namespace App\Service\Cooperative;


use App\Constants\ErrorCode;
use App\Constants\Model\Task as TaskConstants;
use App\Dao\ConfigBannerDao;
use App\Dao\TaskDao;
use App\Exception\BusinessException;
use App\Model\ConfigBanner;
use App\Constants\Model\ConfigBanner as ConfigBannerConstant;
use Hyperf\Di\Annotation\Inject;

class ConfigBannerService extends BaseService
{
    /**
     * @Inject()
     * @var ConfigBannerDao
     */
    protected $configBannerDao;

    /**
     * @Inject()
     * @var TaskDao
     */
    protected $task;

    /**
     *
     */
    public function index()
    {
        $result = $this->configBannerDao->paginate(['cooperative_id' => $this->user()->cooperative_id]);
        if (isset($result[0]->status)){
           $list = $result->toArray();
           $list['current_status'] = $result[0]->status;
        }
        return $list??$result;
    }

    public function formatIndex()
    {
    }

    /**
     * @param $id
     * @param array $data
     * @return bool
     */
    public function save($id, array $data)
    {
        if ($id) {
            /** @var ConfigBanner $configBanner */
            $configBanner = $this->configBannerDao->first(['id' => $id, 'cooperative_id' => $this->user()->cooperative_id]);
            if (!$configBanner) {
                throw new BusinessException(ErrorCode::COOPERATIVE_CONFIG_BANNER_NOT_FOUND);
            }
        } else {
            //banner最大数量不能超过指定数量
            $count = $this->configBannerDao->where(['cooperative_id' => $this->user()->cooperative_id])->count();
            if ($count >= ConfigBannerConstant::MAX_COUNT_OF_BANNER) {
                throw new BusinessException(
                    ErrorCode::COOPERATIVE_CONFIG_BANNER_OVER_LIMIT,
                    sprintf(ErrorCode::getMessage(ErrorCode::COOPERATIVE_CONFIG_BANNER_OVER_LIMIT), ConfigBannerConstant::MAX_COUNT_OF_BANNER)
                );
            }
            $configBanner = new ConfigBanner();
            $configBanner->cooperative_id = $this->user()->cooperative_id;
        }

        $configBanner->fill($data);

        return $configBanner->save();
    }

    /**
     * 删除
     * @param $id
     * @return int
     * @throws \Exception
     */
    public function delete($id): int
    {
        /** @var ConfigBanner $configBanner */
        $configBanner = $this->configBannerDao->first(['id' => $id, 'cooperative_id' => $this->user()->cooperative_id]);
        if (!$configBanner) {
            throw new BusinessException(ErrorCode::COOPERATIVE_CONFIG_BANNER_NOT_FOUND);
        }
        return $configBanner->delete();
    }

    /**
     * 隐藏
     * @param array $ids
     * @return int
     */
    public function block(array $ids): int
    {
        $where = ['cooperative_id' => $this->user()->cooperative_id];
        $where['ids'] = !empty($ids) ? $ids : [];

        return $this->configBannerDao->updateData($where,['status' => ConfigBannerConstant::STATUS_BLOCKED]);
    }

    /**
     * 显示
     * @param array $ids
     * @return int
     */
    public function unblock(array $ids): int
    {
        $where = ['cooperative_id' => $this->user()->cooperative_id];
        $where['ids'] = !empty($ids) ? $ids : [];
        return $this->configBannerDao->updateData($where,['status' => ConfigBannerConstant::STATUS_UNBLOCK]);
    }

    /**
     * 任务，活动页筛选列表
     * @param $type
     * @return mixed
     */
    public function getLinkList($type)
    {
        if ($type == 1){
            $result['data'][] = $this->task->first([
                'cooperative_id' => $this->user()->cooperative_id,
                'show_status' => TaskConstants::SHOW_STATUS_ONLINE,
                'deleted_at'=> null
            ],['id','title']);
        }else{
            $result['data'] = [
                [
                    'id' => '1',
                    'title' => '活动一'
                ],
                [
                    'id' => '2',
                    'title' => '活动二'
                ],
                [
                    'id' => '3',
                    'title' => '活动三'
                ],
                [
                    'id' => '4',
                    'title' => '活动四'
                ],
                [
                    'id' => '5',
                    'title' => '活动五'
                ],
            ];
        }

        return $result;

    }
}